﻿package com.shangda.Server;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;

import org.apache.mina.common.IdleStatus;
import org.apache.mina.common.IoBuffer;
import org.apache.mina.common.IoHandlerAdapter;
import org.apache.mina.common.IoSession;

import com.shangda.Thread.DataBaseSave;
import com.shangda.entity.ReceiveData;
import com.shangda.frame.MainFrame;
import com.shangda.service.DataService;
import com.shangda.util.RequestHandel;

public class MinaBizHandler extends IoHandlerAdapter{
	private DataService dataService;
	private int port;
	private MainFrame mainFrame;
	//记录当前活跃的站点
	
	private List<String> sewageIds;
	//记录站点修改
	private List<Integer> timeChanges;
	private Map<String,Byte> switchStatus;
	//private BlockingQueue<List<ReceiveData>> receiveAll = null; 
	//private DataBaseSave dataBaseSave;
	public MinaBizHandler(DataService dataService,int port,MainFrame mainFrame){
		this.dataService = dataService;
		this.port = port;
		this.mainFrame = mainFrame;
		this.sewageIds = new ArrayList<String>();
		this.timeChanges = new ArrayList<Integer>();
		this.switchStatus = new HashMap<String,Byte>();
		//this.receiveAll  = new LinkedBlockingDeque<List<ReceiveData>>();
		//dataBaseSave = new DataBaseSave(receiveAll, dataService);
		//dataBaseSave.start();
	}
	
	
	
	
	
	
	public void exceptionCaught(IoSession session, Throwable cause)
		      throws Exception {
		    //cause.printStackTrace();
		    session.close(true);
		  }

		  @Override
		  public void sessionCreated(IoSession session) throws Exception {
		    super.sessionCreated(session);
		  }

		  @Override
		  public void sessionOpened(IoSession session) throws Exception {
		    super.sessionOpened(session);
		    
		    //发送连接到下位机  35，1
		    if (port == 8991){
		    	session.write(IoBuffer.wrap(new byte [] {35,1}));//传入 是NIObuffer返回的是IoBuffer
		    }
		    
		    
		  }

		  @Override
		  public void sessionClosed(IoSession session) throws Exception {
		    super.sessionClosed(session);
		    session.close(true);
		  }

		  @Override
		  public void sessionIdle(IoSession session, IdleStatus status)
		      throws Exception {
		    // TODO Auto-generated method stub
			  //System.out.println("kongxian");
		    //System.out.println("IDLE : " + session.getIdleCount(status) + ">now : " + new Date());
		  }

		  @Override
		  public void messageReceived(IoSession session, Object message)
		      throws Exception {
			  
			  IoBuffer temp = (IoBuffer) message;
			  int count = temp.limit();
			  byte [] shows = temp.array();
			  List<ReceiveData> receiveDatas = null ;
			 
			  //商达老系统接受;
			  if (port == 8990){
				  receiveDatas = RequestHandel.dataHandelOrigin(shows, count,session,timeChanges,switchStatus);
				  if (receiveDatas.size() > 0){
					 // receiveAll.put(receiveDatas);
					  
					  synchronized (dataService) {
						  dataService.DataSaveOrgin(receiveDatas);
					}
				  }
			  }
			  
			 //惠山系统接收;
			  if (port == 8992){
				  receiveDatas = RequestHandel.dataHandelOrigin(shows, count,session,timeChanges,switchStatus);
				  if (receiveDatas.size() > 0){
					 // receiveAll.put(receiveDatas);
					  
					  synchronized (dataService) {
						  dataService.DataSaveOrgin(receiveDatas);
					}
				  }
			  }
			  
			//--系统接收;
			  if (port == 8993){
				  receiveDatas = RequestHandel.dataHandelOrigin(shows, count,session,timeChanges,switchStatus);
				  if (receiveDatas.size() > 0){
					 // receiveAll.put(receiveDatas);
					  
					  synchronized (dataService) {
						  dataService.DataSaveOrgin(receiveDatas);
					}
				  }
			  }
			  
			//--系统接收;
			  if (port == 8994){
				  receiveDatas = RequestHandel.dataHandelOrigin(shows, count,session,timeChanges,switchStatus);
				  if (receiveDatas.size() > 0){
					 // receiveAll.put(receiveDatas);
					  
					  synchronized (dataService) {
						  dataService.DataSaveOrgin(receiveDatas);
					}
				  }
			  }
			  
			//--系统接收;
			  if (port == 8995){
				  receiveDatas = RequestHandel.dataHandelJD(shows, count, session, dataService, timeChanges);
					 if (receiveDatas.size() > 0){
						 synchronized (dataService) {
							 dataService.DataSave(receiveDatas);
						}
					 }
			  }
			  
			//--系统接收;
			  if (port == 8996){
				  receiveDatas = RequestHandel.dataHandelOrigin(shows, count,session,timeChanges,switchStatus);
				  if (receiveDatas.size() > 0){
					 // receiveAll.put(receiveDatas);
					  
					  synchronized (dataService) {
						  dataService.DataSaveOrgin(receiveDatas);
					}
				  }
			  }
			  
			//--系统接收;
			  if (port == 8997){
				  receiveDatas = RequestHandel.dataHandelOrigin(shows, count,session,timeChanges,switchStatus);
				  if (receiveDatas.size() > 0){
					 // receiveAll.put(receiveDatas);
					  
					  synchronized (dataService) {
						  dataService.DataSaveOrgin(receiveDatas);
					}
				  }
			  }
			  
			//--系统接收;
			  if (port == 8998){
				  receiveDatas = RequestHandel.dataHandelOrigin(shows, count,session,timeChanges,switchStatus);
				  if (receiveDatas.size() > 0){
					 // receiveAll.put(receiveDatas);
					  
					  synchronized (dataService) {
						  dataService.DataSaveOrgin(receiveDatas);
					}
				  }
			  }
			  
			//--系统接收;
			  if (port == 8999){
				  receiveDatas = RequestHandel.dataHandelOrigin(shows, count,session,timeChanges,switchStatus);
				  if (receiveDatas.size() > 0){
					 // receiveAll.put(receiveDatas);
					  
					  synchronized (dataService) {
						  dataService.DataSaveOrgin(receiveDatas);
					}
				  }
			  }
			  
			  
			  //江都水务接受
			  if (port == 8991){
				  
				 receiveDatas = RequestHandel.dataHandelJD(shows, count, session, dataService, timeChanges);
				 if (receiveDatas.size() > 0){
					 synchronized (dataService) {
						 dataService.DataSave(receiveDatas);
					}
				 }
				  
			  }
			  
			  
			  //面源污染接受
			  if (port == 9990){
				  receiveDatas = RequestHandel.dataHandelAS(shows, count);
				  if (receiveDatas.size() > 0){
					  synchronized (dataService) {
						dataService.DataSaveAS(receiveDatas);
					}
				  }
			  }
			  
			  
			
			 
			  
			 
			  //加入sewageId
			  if (receiveDatas.size() > 0){
				  if (!sewageIds.contains(receiveDatas.get(0).getSewageId()+"")){
					  sewageIds.add(receiveDatas.get(0).getSewageId()+"");
					//显示连接总数
					mainFrame.getTotalConnectionField().setText(sewageIds.size()+"");
						
					mainFrame.getActivitySewage().addItem(receiveDatas.get(0).getSewageId()+"");
				  }
				  
				  //显示sewageid
				  mainFrame.getDatas().setText(receiveDatas.get(0).toString());
				  mainFrame.getSewageIdField().setText(receiveDatas.get(0).getSewageId() +"");
				  
			  }
			
			  
			  
			  InetSocketAddress client = (InetSocketAddress) session.getRemoteAddress();
			  //显示发送端信息
			  mainFrame.getSendIpField().setText(client.getAddress().getHostAddress());
			  mainFrame.getSendPortField().setText(client.getPort()+"");

		  }

		  @Override
		  public void messageSent(IoSession session, Object message) throws Exception {
		    // TODO Auto-generated method stub
		    super.messageSent(session, message);
		    System.out.println("server sent : " + message.toString());
		  }

}
